package com.hyh.fireworks.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyh.fireworks.dto.UserRegisterParam;
import com.hyh.fireworks.entity.User;
import com.hyh.fireworks.exception.CustomException;
import com.hyh.fireworks.mapper.UserMapper;
import com.hyh.fireworks.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.sql.Struct;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author summerday
 * @since 2020-11-07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User login(String username, String password) {
        return this.baseMapper.selectOne(new QueryWrapper<User>()
                .eq("username", username)
                .eq("password", password));
    }

    @Override
    public User register(UserRegisterParam registerParam) {

        //查看是否拥有相同用户名的用户
        User u = getUserByUsername(registerParam.getUsername());
        if(u != null){
            return null;
        }
        User user = new User();
        BeanUtils.copyProperties(registerParam,user);
        this.save(user);
        return user;
    }

    @Override
    public User getUserByUsername(String username) {
        return this.baseMapper.selectOne(
                new QueryWrapper<User>().eq("username",username));
    }

}
